package com.meitianhui.convenitenbuy.module.mine.ui.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.base.BaseToolbarActivity;
import com.meitianhui.convenitenbuy.module.mine.adapter.SystemNotifyAdapter;
import com.meitianhui.convenitenbuy.module.mine.contract.SystemNotifyContract;
import com.meitianhui.convenitenbuy.module.mine.di.DaggerMineComponent;
import com.meitianhui.convenitenbuy.module.mine.di.MineModule;
import com.meitianhui.convenitenbuy.module.mine.model.database.SystemNotifyBean;
import com.meitianhui.convenitenbuy.module.mine.model.database.SystemNotifyBeanDao;
import com.meitianhui.convenitenbuy.module.mine.presenter.SystemNotifyPresenter;
import com.meitianhui.convenitenbuy.util.TipUtils;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
Created by amy on 2017/11/21.
 */

public class SystemNotifyActivity extends BaseToolbarActivity implements SystemNotifyContract.View {
    private LinearLayout mLlSysNone;
    private RecyclerView mRlvSysList;
    @Inject
    SystemNotifyPresenter mSystemNotifyPresenter;
    private List<String> mSysNotifyList = new ArrayList<>();
    private SystemNotifyAdapter mSystemNotifyAdapter;
    private SystemNotifyBeanDao mDao;
    private List<SystemNotifyBean> mSystemNotifyBeanList;

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        DaggerMineComponent.builder().appComponent(App.getInstance().getAppComponent()).mineModule(new MineModule(this)).build().inject(this);
        setTbTitle("系统通知");
        isEnable(false);
        initId();
        initData();
        getSystemNotify();
    }

    private void initData() {
        mSystemNotifyBeanList = new ArrayList<>();

        mDao = App.getInstance().getDaoSession().getSystemNotifyBeanDao();
        mSystemNotifyBeanList = findAllSystemNotify();
        LinearLayoutManager lManager = new LinearLayoutManager(this);
        mSystemNotifyAdapter = new SystemNotifyAdapter(mSystemNotifyBeanList, this);
        mRlvSysList.setLayoutManager(lManager);
        mRlvSysList.setAdapter(mSystemNotifyAdapter);
    }

    private void getSystemNotify() {
        mSystemNotifyPresenter.getSystemNotify();
    }

    private void initId() {
        mLlSysNone = getViewById(R.id.ll_sys_none);
        mRlvSysList = getViewById(R.id.rlv_sys_list);
    }

    @Override
    protected int setView() {
        return R.layout.activity_system_notify;
    }

    @Override
    protected void onRefreshData() {
    }

    @Override
    public void showError(String msg) {
        TipUtils.showToast(App.getInstance(), msg,2);
        mLlSysNone.setVisibility(View.VISIBLE);
        mRlvSysList.setVisibility(View.INVISIBLE);
    }

    @Override
    public void showEmpty() {
        mLlSysNone.setVisibility(View.VISIBLE);
        mRlvSysList.setVisibility(View.INVISIBLE);
    }

    @Override
    public void getSystemNotifySuccess(List<String> sysNotifyList)  {

        if (sysNotifyList.size() == 0){
            mSystemNotifyBeanList = findAllSystemNotify();

        }else{

            for (int i = 0; i < sysNotifyList.size(); i++) {
                SystemNotifyBean systemNotifyBean = new SystemNotifyBean();
                systemNotifyBean.setContent(sysNotifyList.get(i));
                mDao.insertOrReplace(systemNotifyBean);
            }
            mSystemNotifyBeanList = findAllSystemNotify();
        }

        mSystemNotifyAdapter.setNewData(mSystemNotifyBeanList);

    }
    private List<SystemNotifyBean> findAllSystemNotify() {
        return mDao.queryBuilder()
                .list();
    }

}
